import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'  # 去警告
import tensorflow as tf
tf.compat.v1.disable_eager_execution()

def session_demo():
    """
    会话的演示
    :return:
    """
    # 定义常量
    a = tf.constant(2, name="a")
    b = tf.constant(3)
    print("b：\n", b)
    c = tf.add(a, b)

    # 定义占位符
    a_p = tf.compat.v1.placeholder(tf.float32)
    b_p = tf.compat.v1.placeholder(tf.float32)
    c_p = tf.add(a_p, b_p)

    # 默认的图
    default_g = tf.compat.v1.get_default_graph()
    print("默认的图为：\n", default_g)

    # 开启会话
    with tf.compat.v1.Session(config=tf.compat.v1.ConfigProto(allow_soft_placement=True, log_device_placement=True)) as sess:
        print("sess的图属性: \n", sess.graph)

        # 使用 run() 方法计算 c 的值
        abc = sess.run([a,b,c])
        print("abc的结果为: \n", abc)

        # 占位符结果
        c_p_sum = sess.run(c_p, feed_dict={a_p: 3.0, b_p: 4.0})
        print("c_p_sum的结果为: \n", c_p_sum)

    return None

if __name__ == "__main__":
    # 代码5：会话的演示
    session_demo()